package adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.List;

import beans.UserManageBean;
import butterknife.BindView;
import butterknife.OnClick;
import custom.BaseRecyclerAdapter;

/**
 * Created by Administrator on 2018/5/9.
 */

public class UserManageAdapter extends BaseRecyclerAdapter<UserManageAdapter.UserManageViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<UserManageBean> list;
    private OnClickListener onClickListener;

    public UserManageAdapter(Context mContext, List<UserManageBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    public void setList(List<UserManageBean> list) {
        this.list = list;
    }

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public UserManageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final UserManageViewHolder viewHolder = new UserManageViewHolder(inflater.inflate(R.layout.adapter_user_manage, parent, false));
        viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onEdit(viewHolder.getPosition());
            }
        });
        viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onDelete(viewHolder.getPosition());
            }
        });
        viewHolder.tvFingerprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onChangeFingerprint(viewHolder.getPosition());
            }
        });

        viewHolder.tvFingerprint2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onChangeFingerprint2(viewHolder.getPosition());
            }
        });

        viewHolder.tvIris.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onChangeIris(viewHolder.getPosition());
            }
        });
        viewHolder.tvPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onChangePassword(viewHolder.getPosition());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UserManageViewHolder viewHolder, int position) {
        UserManageBean bean = list.get(position);
        if (TextUtils.isEmpty(bean.getNumber())) {
            viewHolder.tvNumber.setText("无");
        }else {
            viewHolder.tvNumber.setText(bean.getNumber());
        }

        if (TextUtils.isEmpty(bean.getName())) {
            viewHolder.tvName.setText("无");
        }else {
            viewHolder.tvName.setText(bean.getName());
        }

        if (TextUtils.isEmpty(bean.getDepartment())) {
            viewHolder.tvDepartment.setText("无");
        }else {
            viewHolder.tvDepartment.setText(bean.getDepartment());
        }

        if (TextUtils.isEmpty(bean.getRole())) {
            viewHolder.tvRole.setText("无");
        }else {
            viewHolder.tvRole.setText(bean.getRole());
        }

        if (TextUtils.isEmpty(bean.getWorkKind())) {
            viewHolder.tvWorkkind.setText("无");
        }else {
            viewHolder.tvWorkkind.setText(bean.getWorkKind());
        }

        if (TextUtils.isEmpty(bean.getLeftImg())) {
            viewHolder.tvIris.setText("新增");
            viewHolder.tvIris.setTextColor(mContext.getResources().getColor(R.color.user_manage_red));
        }else {
            viewHolder.tvIris.setText("修改");
            viewHolder.tvIris.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
        }
        if (TextUtils.isEmpty(bean.getFingerOne())) {
            viewHolder.tvFingerprint.setText("新增");
            viewHolder.tvFingerprint.setTextColor(mContext.getResources().getColor(R.color.user_manage_red));
        }else {
            viewHolder.tvFingerprint.setText("修改");
            viewHolder.tvFingerprint.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
        }

        if (TextUtils.isEmpty(bean.getFingerTwo())) {
            viewHolder.tvFingerprint2.setText("新增");
            viewHolder.tvFingerprint2.setTextColor(mContext.getResources().getColor(R.color.user_manage_red));
        }else {
            viewHolder.tvFingerprint2.setText("修改");
            viewHolder.tvFingerprint2.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
        }

        if (TextUtils.isEmpty(bean.getPassword())){
            viewHolder.tvPassword.setText("新增");
            viewHolder.tvPassword.setTextColor(mContext.getResources().getColor(R.color.user_manage_red));
        }else {
            viewHolder.tvPassword.setText("修改");
            viewHolder.tvPassword.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
        }

        if (position % 2 == 0) {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class UserManageViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_adapter_user_manage_number)
        TextView tvNumber;
        @BindView(R.id.tv_adapter_user_manage_name)
        TextView tvName;
        @BindView(R.id.tv_adapter_user_manage_department)
        TextView tvDepartment;
        @BindView(R.id.tv_adapter_user_manage_role)
        TextView tvRole;
        @BindView(R.id.tv_adapter_user_manage_workkind)
        TextView tvWorkkind;
        @BindView(R.id.tv_adapter_user_manage_edit)
        TextView tvEdit;
        @BindView(R.id.tv_adapter_user_manage_password)
        TextView tvPassword;
        @BindView(R.id.ll_user_manage_title)
        LinearLayout llTitle;
        @BindView(R.id.tv_adapter_user_manage_iris)
        TextView tvIris;
        @BindView(R.id.tv_adapter_user_manage_delete)
        TextView tvDelete;
        @BindView(R.id.tv_adapter_user_manage_fingerprint)
        TextView tvFingerprint;
        @BindView(R.id.tv_adapter_user_manage_fingerprint2)
        TextView tvFingerprint2;

        public UserManageViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnClickListener {
        void onChangeIris(int position);

        void onChangeFingerprint(int position);

        void onChangeFingerprint2(int position);

        void onEdit(int position);

        void onChangePassword(int position);

        void onDelete(int position);
    }
}
